/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.google.common.collect.Maps;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.soap.SoapProtocol;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraCookie;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.accesscontrol.AdminRight;
/*     */ import com.zimbra.cs.account.accesscontrol.Rights.Admin;
/*     */ import com.zimbra.soap.JaxbUtil;
/*     */ import com.zimbra.soap.admin.message.AuthRequest;
/*     */ import com.zimbra.soap.admin.message.AuthResponse;
/*     */ import com.zimbra.soap.admin.message.DeployZimletRequest;
/*     */ import com.zimbra.soap.admin.message.DeployZimletResponse;
/*     */ import com.zimbra.soap.admin.message.UndeployZimletRequest;
/*     */ import com.zimbra.soap.admin.message.UndeployZimletResponse;
/*     */ import com.zimbra.soap.admin.type.AttachmentIdAttrib;
/*     */ import com.zimbra.soap.admin.type.ZimletDeploymentStatus;
/*     */ import java.io.File;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.httpclient.Cookie;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpState;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ import org.apache.commons.httpclient.methods.multipart.FilePart;
/*     */ import org.apache.commons.httpclient.methods.multipart.FilePartSource;
/*     */ import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
/*     */ import org.apache.commons.httpclient.methods.multipart.Part;
/*     */ import org.apache.commons.httpclient.params.HttpClientParams;
/*     */ import org.junit.After;
/*     */ import org.junit.AfterClass;
/*     */ import org.junit.Assert;
/*     */ import org.junit.Before;
/*     */ import org.junit.BeforeClass;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestDeployZimlet
/*     */ {
/*     */   private static String ADMIN_UPLOAD_URL;
/*     */   private static Provisioning prov;
/*     */   private static Server localServer;
/*     */   private static Account delegatedAdmin;
/*  59 */   private static String DELEGATED_ADMIN = "TestDeployZimletDelegatedAdmin";
/*     */   
/*     */   @BeforeClass
/*  62 */   public static void init() throws Exception { int port = 7071;
/*  63 */     prov = Provisioning.getInstance();
/*  64 */     localServer = prov.getLocalServer();
/*  65 */     port = localServer.getIntAttr("zimbraAdminPort", 0);
/*  66 */     ADMIN_UPLOAD_URL = "https://" + localServer.getServiceHostname() + ":" + port + "/service/upload?fmt=raw";
/*  67 */     Map<String, Object> attrs = Maps.newHashMap();
/*  68 */     attrs.put("zimbraIsDelegatedAdminAccount", "TRUE");
/*  69 */     attrs.put("zimbraAdminConsoleUIComponents", "accountListView");
/*  70 */     attrs.put("zimbraAdminConsoleUIComponents", "downloadsView");
/*  71 */     attrs.put("zimbraAdminConsoleUIComponents", "DLListView");
/*     */     
/*  73 */     delegatedAdmin = TestUtil.createAccount(TestUtil.addDomainIfNecessary(DELEGATED_ADMIN), attrs);
/*  74 */     TestUtil.grantRightToAdmin(TestUtil.newSoapProvisioning(), com.zimbra.soap.type.TargetType.fromString(com.zimbra.cs.account.accesscontrol.TargetType.server.toString()), localServer.getName(), delegatedAdmin.getName(), Rights.Admin.R_deployZimlet.getName());
/*     */     
/*  76 */     File rogueFile = new File("/opt/zimbra/conf/rogue.file");
/*  77 */     if (rogueFile.exists()) {
/*  78 */       rogueFile.delete();
/*     */     }
/*  80 */     File legitFile = new File("/opt/zimbra/zimlets-deployed/absolute/opt/zimbra/conf/rogue.file");
/*  81 */     if (legitFile.exists()) {
/*  82 */       legitFile.delete();
/*     */     }
/*     */   }
/*     */   
/*     */   @AfterClass
/*     */   public static void after() throws Exception {
/*  88 */     if (TestUtil.accountExists(DELEGATED_ADMIN)) {
/*  89 */       TestUtil.deleteAccount(DELEGATED_ADMIN);
/*     */     }
/*     */   }
/*     */   
/*     */   @Before
/*     */   public void setUp() throws Exception
/*     */   {}
/*     */   
/*     */   @After
/*     */   public void tearDown() throws Exception
/*     */   {}
/*     */   
/*     */   @Test
/*     */   public void testValidZimlet() throws Exception {
/* 103 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 104 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 106 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 107 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 108 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 109 */     String authToken = authResp.getAuthToken();
/*     */     
/* 111 */     String aid = adminUpload(authToken, "com_zimbra_mailarchive.zip", "/opt/zimbra/zimlets/com_zimbra_mailarchive.zip");
/* 112 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 114 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 115 */     transport.setAdmin(true);
/* 116 */     transport.setAuthToken(authToken);
/* 117 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 118 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/* 119 */     Element res = transport.invoke(req);
/* 120 */     DeployZimletResponse deployResp = (DeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 121 */     Assert.assertNotNull(deployResp);
/* 122 */     String status = ((ZimletDeploymentStatus)deployResp.getProgresses().get(0)).getStatus();
/* 123 */     Assert.assertTrue("should be getting 'pending' or 'succeeded' status", (status.equals("pending")) || (status.equals("succeeded")));
/*     */     
/*     */ 
/* 126 */     int waitMs = 10000;
/* 127 */     while (waitMs > 0) {
/* 128 */       DeployZimletRequest statusReq = new DeployZimletRequest("status", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 129 */       req = JaxbUtil.jaxbToElement(statusReq);
/* 130 */       res = transport.invoke(req);
/* 131 */       DeployZimletResponse statusResp = (DeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 132 */       Assert.assertNotNull(statusResp);
/* 133 */       status = ((ZimletDeploymentStatus)statusResp.getProgresses().get(0)).getStatus();
/* 134 */       Assert.assertTrue("should be getting 'pending' or 'succeeded' status", (status.equals("pending")) || (status.equals("succeeded")));
/* 135 */       if (status.equals("succeeded")) {
/*     */         break;
/*     */       }
/* 138 */       Thread.sleep(500L);
/* 139 */       waitMs -= 500;
/*     */     }
/* 141 */     Assert.assertTrue("should be getting 'succeeded' status. Status is: " + status, status.equals("succeeded"));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testEmptyAid() throws Exception {
/* 146 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 147 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 149 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 150 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 151 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 152 */     String authToken = authResp.getAuthToken();
/* 153 */     transport.setAdmin(true);
/* 154 */     transport.setAuthToken(authToken);
/* 155 */     AttachmentIdAttrib att = new AttachmentIdAttrib("");
/* 156 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 157 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 159 */       Element res = transport.invoke(req);
/* 160 */       JaxbUtil.elementToJaxb(res);
/* 161 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testNoAid() throws Exception
/*     */   {
/* 169 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 170 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 172 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 173 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 174 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 175 */     String authToken = authResp.getAuthToken();
/* 176 */     transport.setAdmin(true);
/* 177 */     transport.setAuthToken(authToken);
/* 178 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), null);
/* 179 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 181 */       Element res = transport.invoke(req);
/* 182 */       JaxbUtil.elementToJaxb(res);
/* 183 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testBadAid() throws Exception
/*     */   {
/* 191 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 192 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 194 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 195 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 196 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 197 */     String authToken = authResp.getAuthToken();
/*     */     
/* 199 */     AttachmentIdAttrib att = new AttachmentIdAttrib("invalidaid");
/* 200 */     transport.setAdmin(true);
/* 201 */     transport.setAuthToken(authToken);
/* 202 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 203 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 205 */       Element res = transport.invoke(req);
/* 206 */       JaxbUtil.elementToJaxb(res);
/* 207 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testInvalidAction() throws Exception
/*     */   {
/* 215 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 216 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 218 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 219 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 220 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 221 */     String authToken = authResp.getAuthToken();
/*     */     
/* 223 */     String aid = adminUpload(authToken, "com_zimbra_mailarchive.zip", "/opt/zimbra/zimlets/com_zimbra_mailarchive.zip");
/* 224 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 226 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 227 */     transport.setAdmin(true);
/* 228 */     transport.setAuthToken(authToken);
/* 229 */     DeployZimletRequest deployReq = new DeployZimletRequest("invalidaction", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 230 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 232 */       Element res = transport.invoke(req);
/* 233 */       JaxbUtil.elementToJaxb(res);
/* 234 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testBadZimletName() throws Exception
/*     */   {
/* 242 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 243 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 245 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 246 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 247 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 248 */     String authToken = authResp.getAuthToken();
/* 249 */     String aid = adminUpload(authToken, "attack.zip", "/opt/zimbra/data/unittest/zimlets/attack.zip");
/* 250 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 252 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 253 */     transport.setAdmin(true);
/* 254 */     transport.setAuthToken(authToken);
/* 255 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 256 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 258 */       Element res = transport.invoke(req);
/* 259 */       JaxbUtil.elementToJaxb(res);
/* 260 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testValidAdminExtension() throws Exception
/*     */   {
/* 268 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 269 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 271 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 272 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 273 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 274 */     String authToken = authResp.getAuthToken();
/* 275 */     String aid = adminUpload(authToken, "adminextension.zip", "/opt/zimbra/data/unittest/zimlets/adminextension.zip");
/* 276 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 278 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 279 */     transport.setAdmin(true);
/* 280 */     transport.setAuthToken(authToken);
/* 281 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 282 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/* 283 */     Element res = transport.invoke(req);
/* 284 */     DeployZimletResponse deployResp = (DeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 285 */     Assert.assertNotNull(deployResp);
/* 286 */     String status = ((ZimletDeploymentStatus)deployResp.getProgresses().get(0)).getStatus();
/* 287 */     Assert.assertTrue("should be getting 'pending' or 'succeeded' status", (status.equals("pending")) || (status.equals("succeeded")));
/*     */     
/* 289 */     UndeployZimletRequest undeployReq = new UndeployZimletRequest("adminextension", "deployall");
/* 290 */     req = JaxbUtil.jaxbToElement(undeployReq);
/* 291 */     res = transport.invoke(req);
/* 292 */     UndeployZimletResponse undeployResp = (UndeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 293 */     Assert.assertNotNull(undeployResp);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testAdminExtensionDelegatedAdmin() throws Exception {
/* 298 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 299 */     AuthRequest authReq = new AuthRequest(delegatedAdmin.getName(), "test123");
/*     */     
/* 301 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 302 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 303 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 304 */     String authToken = authResp.getAuthToken();
/* 305 */     String aid = adminUpload(authToken, "adminextension.zip", "/opt/zimbra/data/unittest/zimlets/adminextension.zip");
/* 306 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 308 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 309 */     transport.setAdmin(true);
/* 310 */     transport.setAuthToken(authToken);
/* 311 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 312 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 314 */       Element res = transport.invoke(req);
/* 315 */       DeployZimletResponse deployResp = (DeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 316 */       Assert.fail("Should throw SoapFaultException. Instead received " + deployResp.toString());
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testUndeployBadName() throws Exception
/*     */   {
/* 324 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 325 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 327 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 328 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 329 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 330 */     String authToken = authResp.getAuthToken();
/* 331 */     transport.setAdmin(true);
/* 332 */     transport.setAuthToken(authToken);
/* 333 */     UndeployZimletRequest undeployReq = new UndeployZimletRequest("../data/something", "deployall");
/* 334 */     Element req = JaxbUtil.jaxbToElement(undeployReq);
/*     */     try {
/* 336 */       Element res = transport.invoke(req);
/* 337 */       UndeployZimletResponse undeployResp = (UndeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 338 */       Assert.fail("Should throw SoapFaultException. Instead received " + undeployResp.toString());
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testZimletDelegatedAdmin() throws Exception
/*     */   {
/* 346 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 347 */     AuthRequest authReq = new AuthRequest(delegatedAdmin.getName(), "test123");
/*     */     
/* 349 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 350 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 351 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 352 */     String authToken = authResp.getAuthToken();
/* 353 */     String aid = adminUpload(authToken, "com_zimbra_mailarchive.zip", "/opt/zimbra/zimlets/com_zimbra_mailarchive.zip");
/* 354 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 356 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 357 */     transport.setAdmin(true);
/* 358 */     transport.setAuthToken(authToken);
/* 359 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 360 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/* 361 */     Element res = transport.invoke(req);
/* 362 */     DeployZimletResponse deployResp = (DeployZimletResponse)JaxbUtil.elementToJaxb(res);
/* 363 */     Assert.assertNotNull(deployResp);
/* 364 */     String status = ((ZimletDeploymentStatus)deployResp.getProgresses().get(0)).getStatus();
/* 365 */     Assert.assertTrue("should be getting 'pending' or 'succeeded' status", (status.equals("pending")) || (status.equals("succeeded")));
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testZipWithTraversal() throws Exception
/*     */   {
/* 371 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 372 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 374 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 375 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 376 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 377 */     String authToken = authResp.getAuthToken();
/* 378 */     String aid = adminUpload(authToken, "attack.zip", "/opt/zimbra/data/unittest/zimlets/com_zimbra_url.zip");
/* 379 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 381 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 382 */     transport.setAdmin(true);
/* 383 */     transport.setAuthToken(authToken);
/* 384 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 385 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 387 */       Element res = transport.invoke(req);
/* 388 */       JaxbUtil.elementToJaxb(res);
/* 389 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testZipWithInvalidCharacter() throws Exception
/*     */   {
/* 397 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 398 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 400 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 401 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 402 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 403 */     String authToken = authResp.getAuthToken();
/* 404 */     String aid = adminUpload(authToken, "jelmer.zip", "/opt/zimbra/data/unittest/zimlets/jelmer.zip");
/* 405 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 407 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 408 */     transport.setAdmin(true);
/* 409 */     transport.setAuthToken(authToken);
/* 410 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 411 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 413 */       Element res = transport.invoke(req);
/* 414 */       JaxbUtil.elementToJaxb(res);
/* 415 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testZipWithAbsolutePath() throws Exception
/*     */   {
/* 423 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 424 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 426 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 427 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 428 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 429 */     String authToken = authResp.getAuthToken();
/* 430 */     String aid = adminUpload(authToken, "absolute.zip", "/opt/zimbra/data/unittest/zimlets/absolute.zip");
/* 431 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 433 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 434 */     transport.setAdmin(true);
/* 435 */     transport.setAuthToken(authToken);
/* 436 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 437 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 439 */       Element res = transport.invoke(req);
/* 440 */       JaxbUtil.elementToJaxb(res);
/* 441 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */     
/*     */ 
/*     */ 
/* 447 */     File rogueFile = new File("/opt/zimbra/conf/rogue.file");
/* 448 */     Assert.assertFalse("/opt/zimbra/conf/rogue.file should not have been created", rogueFile.exists());
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testPhilsZip() throws Exception {
/* 453 */     SoapHttpTransport transport = new SoapHttpTransport(TestUtil.getAdminSoapUrl());
/* 454 */     AuthRequest authReq = new AuthRequest(LC.zimbra_ldap_user.value(), LC.zimbra_ldap_password.value());
/*     */     
/* 456 */     authReq.setCsrfSupported(Boolean.valueOf(false));
/* 457 */     Element response = transport.invoke(JaxbUtil.jaxbToElement(authReq, SoapProtocol.SoapJS.getFactory()));
/* 458 */     AuthResponse authResp = (AuthResponse)JaxbUtil.elementToJaxb(response);
/* 459 */     String authToken = authResp.getAuthToken();
/* 460 */     String aid = adminUpload(authToken, "phil.zip", "/opt/zimbra/data/unittest/zimlets/phil.zip");
/* 461 */     Assert.assertNotNull("Attachment ID should not be null", aid);
/*     */     
/* 463 */     AttachmentIdAttrib att = new AttachmentIdAttrib(aid);
/* 464 */     transport.setAdmin(true);
/* 465 */     transport.setAuthToken(authToken);
/* 466 */     DeployZimletRequest deployReq = new DeployZimletRequest("deploylocal", Boolean.valueOf(false), Boolean.valueOf(true), att);
/* 467 */     Element req = JaxbUtil.jaxbToElement(deployReq);
/*     */     try {
/* 469 */       Element res = transport.invoke(req);
/* 470 */       JaxbUtil.elementToJaxb(res);
/* 471 */       Assert.fail("Should throw SoapFaultException");
/*     */     }
/*     */     catch (SoapFaultException e) {}
/*     */   }
/*     */   
/*     */   public String adminUpload(String authToken, String fileName, String filePath) throws Exception
/*     */   {
/* 478 */     PostMethod post = new PostMethod(ADMIN_UPLOAD_URL);
/* 479 */     FilePart part = new FilePart(fileName, new FilePartSource(new File(filePath)));
/* 480 */     String contentType = "application/x-msdownload";
/* 481 */     part.setContentType(contentType);
/* 482 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/* 483 */     HttpState state = new HttpState();
/* 484 */     state.addCookie(new Cookie(localServer.getServiceHostname(), ZimbraCookie.authTokenCookieName(true), authToken, "/", null, false));
/*     */     
/* 486 */     client.getParams().setCookiePolicy("compatibility");
/* 487 */     client.setState(state);
/* 488 */     post.setRequestEntity(new MultipartRequestEntity(new Part[] { part }, post.getParams()));
/* 489 */     int statusCode = HttpClientUtil.executeMethod(client, post);
/* 490 */     Assert.assertEquals("This request should succeed. Getting status code " + statusCode, 200L, statusCode);
/* 491 */     String resp = post.getResponseBodyAsString();
/* 492 */     Assert.assertNotNull("Response should not be empty", resp);
/* 493 */     ZimbraLog.test.debug("Upload response " + resp);
/* 494 */     String[] responseParts = resp.split(",", 3);
/* 495 */     String aid = null;
/* 496 */     if (responseParts.length == 3) {
/* 497 */       aid = responseParts[2].trim();
/* 498 */       if ((aid.startsWith("'")) || (aid.startsWith("\""))) {
/* 499 */         aid = aid.substring(1);
/*     */       }
/* 501 */       if ((aid.endsWith("'")) || (aid.endsWith("\""))) {
/* 502 */         aid = aid.substring(0, aid.length() - 1);
/*     */       }
/*     */     }
/* 505 */     return aid;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestDeployZimlet.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */